<?php

namespace app\logic;

class PayPassword
{
    private $obj_model_name;

    private $str_model_name;

    private $str_id_name;

    public function __construct($str_model_name)
    {
        $this->str_model_name = $str_model_name;
        switch ($str_model_name) {
            case 'user': // 用户端
                $this->obj_model_name = \app\model\UserInfo::getInstance();
                $this->str_id_name = 'user_id';
                break;
            default:
                return false;
        }
    }

    /**
     * 设置支付密码
     * @param $int_id
     * @param $str_pay_password
     * @param $str_verify_code
     * @param string $str_pass_key
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function setUpPayPwd($int_id, $str_pay_password, $str_verify_code, $str_pass_key = '')
    {
        _toValidate('pay_pwd', $str_pay_password, 10819);

        _toValidate('verify_code', $str_verify_code, 10010);

        $str_mobile = $this->obj_model_name->toValue(['id' => $int_id], 'mobile');

        // 验证短信验证码是否正确
        $bool_result = \app\logic\sms\SMS::getInstance()->verifySMS('pay_password', $str_mobile, $str_pass_key, $str_verify_code, $this->str_model_name);
        _toCompare($bool_result, '===', false, 13015);

        $this->obj_model_name->setPayPassword($int_id, $str_pay_password);

        _returnData();
    }

    /**
     * 检查支付密码是否正确
     * @param $int_id
     * @param $str_pay_password
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function checkPayPasswordIsRight($int_id, $str_pay_password)
    {
        $bool_result = $this->obj_model_name->checkPayPasswordIsRight($int_id, $str_pay_password);

        _toCompare($bool_result, '!=', true, 10016);

        return true;
    }

    /**
     * 检查是否设置支付密码
     * @param $int_id
     * @return int 0-未设置 1-已设置
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function checkIsSetPayPassword($int_id)
    {
        return $this->obj_model_name->checkIsSetPayPassword($int_id);
    }
}